package com.zlp.netty.component;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import lombok.extern.slf4j.Slf4j;

/**
 * @author: zhulaiping
 * @time: 2022/9/1 16:56
 */
@Slf4j
public class NettyPipelineDemo {


    public static void main(String[] args) {
        new ServerBootstrap()
                .group(new NioEventLoopGroup())
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        //1.通过channel 拿到通道,
                        ChannelPipeline pipeline = ch.pipeline();
                        //2.添加处理器 head tail之间,handler实际是一个双向链表
                        pipeline.addLast("h1",new ChannelInboundHandlerAdapter(){
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                log.debug("1");
                                //底层调用了fireChannelRead方法，将数据传递给下一个处理器
                                super.channelRead(ctx,msg);
                            }
                        });
                        pipeline.addLast("h2",new ChannelInboundHandlerAdapter(){
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                log.debug("2");
                                super.channelRead(ctx,msg);
                            }
                        });
                        pipeline.addLast("h3",new ChannelInboundHandlerAdapter(){
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                log.debug("3");
                                super.channelRead(ctx,msg);
                                //向channel中写入数据传递给客户端，若是channel调用，就直接从tail处开始向前查询出战处理器
                                ch.writeAndFlush(ctx.alloc().buffer().writeBytes("helli".getBytes()));
                            }
                        });
                        pipeline.addLast("h4",new ChannelOutboundHandlerAdapter(){
                            @Override
                            public void write(ChannelHandlerContext ctx, Object msg,ChannelPromise promise) throws Exception {
                                log.debug("4");
                                //将数据传递给下一个处理器
                                super.write(ctx,msg,promise);
                            }
                        });
                        pipeline.addLast("h5",new ChannelOutboundHandlerAdapter(){
                            @Override
                            public void write(ChannelHandlerContext ctx, Object msg,ChannelPromise promise) throws Exception {
                                log.debug("5");
                                super.write(ctx,msg,promise);
                            }
                        });
                        pipeline.addLast("h6",new ChannelOutboundHandlerAdapter(){
                            @Override
                            public void write(ChannelHandlerContext ctx, Object msg,ChannelPromise promise) throws Exception {
                                log.debug("6");
                                super.write(ctx,msg,promise);
                            }
                        });
                    }
                }).bind(8080);
    }
}
